using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Configuration;
namespace DAL
{
    public class tbCTDealDAL
    {
        private SqlConnection cn;
        public tbCTDealDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public tbCTDealDAL(tbCTDealDAL ctdeal)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbCTDeal ctdeal)
        {
            SqlCommand cm = new SqlCommand("tbCTDeal_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madeal", ctdeal.madeal);
            cm.Parameters.AddWithValue("@tinhtrangdeal", ctdeal.tinhtrangdeal);
            cm.Parameters.AddWithValue("@donhang", ctdeal.donhang);
            cm.ExecuteNonQuery();
        }
        public void Update(tbCTDeal ctdeal)
        {
            SqlCommand cm = new SqlCommand("tbCTDeal_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madeal", ctdeal.madeal);
            cm.Parameters.AddWithValue("@tinhtrangdeal", ctdeal.tinhtrangdeal);
            cm.Parameters.AddWithValue("@donhang", ctdeal.donhang);
            cm.ExecuteNonQuery();
        }
        public void Delete(String iD)
        {
            SqlCommand cm = new SqlCommand("tbCTDeal", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madeal", iD);
            cm.ExecuteNonQuery();
        }
        public tbCTDealCollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbCTDeal_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbCTDealCollection ctdealCollection = new tbCTDealCollection();

            while (dr.Read())
            {
                tbCTDeal ctdeal = new tbCTDeal();
                ctdeal.madeal = dr.GetValue(0).ToString();
                ctdeal.tinhtrangdeal = int.Parse(dr.GetValue(1).ToString());
                ctdeal.donhang = int.Parse(dr.GetValue(2).ToString());
                ctdealCollection.Add(ctdeal);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ctdealCollection;
        }

        public void Insert()
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
